<?php

namespace Ignus\View\Extension;

use Ignus\View\Extension;
use Ignus\View\TokenParser\AutoEscape;
use Ignus\View\Filter\FunctionFilter;

class Escaper extends Extension
{
    protected $autoescape;

    public function __construct($autoescape = true)
    {
        $this->autoescape = $autoescape;
    }

    public function getTokenParsers()
    {
        return array(new AutoEscape());
    }

    public function getNodeVisitors()
    {
        return array(new \Ignus\View\NodeVisitor\Escaper());
    }

    public function getFilters()
    {
        return array(
            'raw' => new FunctionFilter('\Ignus\View\Extension\twig_raw_filter', array('is_safe' => array('all'))),
        );
    }

    public function isGlobal()
    {
        return $this->autoescape;
    }

    public function getName()
    {
        return 'escaper';
    }
}

// tells the escaper node visitor that the string is safe
function twig_raw_filter($string)
{
    return $string;
}
